class Solution(object):
    def numIslands(self, grid):
        """
        :type grid: List[List[str]]
        :rtype: int
        """
        nRow = len(grid)
        if nRow==0: return 0
        nCol = len(grid[0])
        def traverse(r, c):
            if not(0<=r<nRow and 0<=c<nCol): return
            if grid[r][c]=='0': return
            grid[r][c]='0'
            traverse(r-1,c)
            traverse(r+1,c)
            traverse(r,c-1)
            traverse(r,c+1)
        cnt=0
        for r in range(nRow):
            for c in range(nCol):
                if grid[r][c]=='1':
                    cnt+=1
                    traverse(r,c)
        return cnt

print(Solution().numIslands())            